package com.sanqing.model;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class PopupMenu {
    private JTable table;
    private JPopupMenu popupMenu;
    private String[] columnNames;
    private java.awt.Component parentComponent; // 用于显示对话框的父组件

    // 修改构造函数，添加父组件参数
    public PopupMenu(JTable table, java.awt.Component parentComponent, String[] columnNames) {
        this.table = table;
        this.parentComponent = parentComponent;
        this.columnNames = columnNames;
        this.popupMenu = new JPopupMenu(); // 直接初始化，不通过参数传入
        initPopupMenu();
    }

    public void initPopupMenu() {
        JMenuItem copyISBNItem = new JMenuItem("复制ISBN");
        copyISBNItem.addActionListener(e -> copySelectedRowISBN());
        popupMenu.add(copyISBNItem);
        
        JMenuItem copyBookNameItem = new JMenuItem("复制图书名称");
        copyBookNameItem.addActionListener(e -> copySelectedRowField(2));
        popupMenu.add(copyBookNameItem);
        
        JMenuItem copyCategoryItem = new JMenuItem("复制图书类别");
        copyCategoryItem.addActionListener(e -> copySelectedRowField(1));
        popupMenu.add(copyCategoryItem);
        
        JMenuItem copyAuthorItem = new JMenuItem("复制作者");
        copyAuthorItem.addActionListener(e -> copySelectedRowField(3));
        popupMenu.add(copyAuthorItem);
        
        JMenuItem copyTranslatorItem = new JMenuItem("复制译者");
        copyTranslatorItem.addActionListener(e -> copySelectedRowField(4));
        popupMenu.add(copyTranslatorItem);
        
        JMenuItem copyPublisherItem = new JMenuItem("复制出版商");
        copyPublisherItem.addActionListener(e -> copySelectedRowField(5));
        popupMenu.add(copyPublisherItem);
        
        JMenuItem copyLanguageItem = new JMenuItem("复制语言");
        copyLanguageItem.addActionListener(e -> copySelectedRowField(6));
        popupMenu.add(copyLanguageItem);
    }
    
    private void copySelectedRowISBN() {
        copySelectedRowField(0);
    }
    
    private void copySelectedRowField(int columnIndex) {
        int selRow = table.getSelectedRow();
        if (selRow < 0) {
            // 使用parentComponent而不是this
            JOptionPane.showMessageDialog(parentComponent, "请先选择一行数据", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        String fieldValue = table.getValueAt(selRow, columnIndex).toString().trim();
        StringSelection selection = new StringSelection(fieldValue);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
        
        String columnName = columnNames[columnIndex];
        JOptionPane.showMessageDialog(parentComponent, columnName + "已复制到剪贴板: " + fieldValue, "成功", JOptionPane.INFORMATION_MESSAGE);
    }
    
    // 提供获取TableListener实例的方法
    public MouseAdapter getTableListener() {
        return new TableListener();
    }
    
    public class TableListener extends MouseAdapter {
        public void mouseClicked(final MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1) {
                int selRow = table.rowAtPoint(e.getPoint());
                if (selRow >= 0) {
                    table.setRowSelectionInterval(selRow, selRow);
                    showBookDetails(selRow);
                }
            } 
            else if (e.getButton() == MouseEvent.BUTTON3) {
                int selRow = table.rowAtPoint(e.getPoint());
                if (selRow >= 0) {
                    table.setRowSelectionInterval(selRow, selRow);
                    popupMenu.show(table, e.getX(), e.getY());
                }
            }
        }
        
        private void showBookDetails(int selRow) {
            StringBuilder details = new StringBuilder();
            
            // 修正列索引，确保不超出范围
            int columnCount = table.getColumnCount();
            if (columnCount > 0) details.append("图书编号: ").append(table.getValueAt(selRow, 0)).append("\n");
            if (columnCount > 1) details.append("图书类别: ").append(table.getValueAt(selRow, 1)).append("\n");
            if (columnCount > 2) details.append("图书名称: ").append(table.getValueAt(selRow, 2)).append("\n");
            if (columnCount > 3) details.append("作者: ").append(table.getValueAt(selRow, 3)).append("\n");
            if (columnCount > 4) details.append("译者: ").append(table.getValueAt(selRow, 4)).append("\n");
            if (columnCount > 5) details.append("出版商: ").append(table.getValueAt(selRow, 5)).append("\n");
            if (columnCount > 6) details.append("语言: ").append(table.getValueAt(selRow, 6)).append("\n");
            if (columnCount > 7) details.append("出版日期: ").append(table.getValueAt(selRow, 7)).append("\n");
            if (columnCount > 8) details.append("价格: ").append(table.getValueAt(selRow, 8)).append("\n");
            if (columnCount > 9) details.append("库存数量: ").append(table.getValueAt(selRow, 9)).append("\n");
            
            // 使用SwingUtilities获取顶层窗口作为父组件
            JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(table), 
                    details.toString(), "图书详情", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}